package com.zzq.springboot1.controller;

import com.zzq.springboot1.Dao.Departmentdao;
import com.zzq.springboot1.Dao.Employeedao;
import com.zzq.springboot1.pojo.Department;
import com.zzq.springboot1.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Generated;
import javax.servlet.http.HttpSession;
import java.util.Collection;

@Controller
public class Employeecontroller {

    @Autowired
    Employeedao employeedao;
    @Autowired
    Departmentdao departmentdao;

    @RequestMapping("/emp")
    public String List(Model model) {
        Collection<Employee> Employees = employeedao.getall();
        model.addAttribute("emp", Employees);
        return "emp/list";


    }

    @GetMapping("/add")
    public String toadd(Model model) {
        //查出所有部门信息
        Collection<Department> departments = departmentdao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/add";
    }

    @PostMapping("/emps")
    public String addemp(Employee employee) {
        //具体业务
        employeedao.save(employee);
        return "redirect:/emp";
    }
        @GetMapping("/emp/{id}")
    public String toupdate(@PathVariable("id") Integer id, Model model){
            Employee employee = employeedao.getEmployeeById(id);
            model.addAttribute("emp",employee);
            //查询部门
            Collection<Department> departments = departmentdao.getDepartments();
            model.addAttribute("departments", departments);
            return"emp/update";
        }
    @PostMapping("/updateemp")
    public String update(Employee employee){
        employeedao.save(employee);
        return "redirect:/emp";
    }

    @GetMapping("/delteemp/{id}")
    public String delte(@PathVariable("id") Integer id){
        employeedao.delete(id);
        return "redirect:/emp";
    }
        @RequestMapping("/user/login")
    public String sigout(HttpSession session){
        session.invalidate();
        return "redirect:/";
    }
}